<?php
class Admin extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }
    
    private function _is() {
        if (!isset($_SESSION['userinfo'])) {
            return FALSE;
        } else {
            $user_id = $_SESSION['userinfo']['user_id'];
            return $this->user_model->is_admin($user_id);
        }
    }
    
    public function index() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->view('header');
            $this->load->view('admin/admin_navbar');
            $this->load->view('admin/admin');
            $this->load->view('footer');
        }
    }
    
    public function feedback() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('feedback_model');
            $fbs = $this->feedback_model->get();
            $data['fbs'] = $fbs;
            
            $this->load->view('header');
            $this->load->view('admin/admin_navbar');            
            $this->load->view('admin/admin_feedback', $data);
            $this->load->view('footer');
        }
    }
    
    public function feedback_handle() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('feedback_model');
            $fb_id = $this->input->post('fb_id');
            $res = $this->feedback_model->handle($fb_id);
            $this->output->set_output($res);
        }
    }
    
    public function slide() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('slide_model');
            $data['slides'] = $this->slide_model->get();
            
            $this->load->view('header');
            $this->load->view('admin/admin_navbar');            
            $this->load->view('admin/admin_slide', $data);
            $this->load->view('footer');
        }
    }
    
    public function slide_update() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('slide_model');
            $slides = $this->input->post('slides');
            $res = $this->slide_model->update($slides);
            // do real resize job
            $this->load->helper('photo');
            $this->load->model('photo_model');
            $photo_ids = array(
                $slides[0]['slide_cover_id'],
                $slides[1]['slide_cover_id'],
                $slides[2]['slide_cover_id'],
                $slides[3]['slide_cover_id']
            );
            $photos = $this->photo_model->get_in($photo_ids);
            if ($photos != NULL) {
                foreach ($photos as $photo) {
                    photo_resize($photo['location'], 143, 107, true, 'height');
                    photo_resize($photo['location'], 572, 429, true, 'height');
                }
            }            
            $this->output->set_output($res);
        }
    }
    
    public function slide_cover_carousel() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('post_photo_model');
            $post_id = $this->input->post('post_id');
            $res = $this->post_photo_model->get($post_id);
            $this->output->set_content_type('application/json')
                         ->set_output(json_encode($res));    
        }
    }
    
    public function post() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->view('header');
            $this->load->view('admin/admin_navbar');            
            $this->load->view('admin/admin_post');
            $this->load->view('footer');
        }    
    }
    
    public function cover($post_id) {
        if (!$this->_is()) {
            show_404();
        } else {
            $data['mode'] = 'admin';
            $data['post_id'] = $post_id;
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('post/cover_select', $data);
            $this->load->view('footer');
        }
    }

    public function post_delete() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('post_model');
            $post_id = $this->input->post('post_id');
            $res = $this->post_model->delete($post_id);            
            $this->output->set_output($res);
        }      
    }

    public function repost_delete() {
        if (!$this->_is()) {
            show_404();
        } else {
            $this->load->model('repost_model');
            $repost_id = $this->input->post('repost_id');
            $res = $this->repost_model->delete($repost_id);
            $this->output->set_output($res);
        }
    }
}
?>